package com.cennavi.minenavidemo.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.cennavi.minenavidemo.R;

/**
 * 自定义标题栏
 *
 *         <p>
 *         自定义属性：
 *         title_name				标题栏标题
 *         title_background			标题栏背景
 *         right_src				标题栏右功能区图片
 *         right_text				标题栏右功能区文字
 *         <p>
 *         right_src 和 right_text  不能同时在布局中出现，默认只显示一个
 */
public class CommonTitleBar extends LinearLayout implements View.OnClickListener {

	private static final String TAG = "CommonTitleBar";
	private Context mContext;
	private LinearLayout mLlTitle;
	private ImageView mIvBack;
	private TextView mTvTitle;
	private View mSpace;

	private RelativeLayout mCustomTitleContainer;
	private ImageView mRightImage;
	private TextView mRightTextView;

	//自定义属性
	private String mTitleName;
	private int mBackgroundColor;
	private int mRightImageSrcRes;
	private String mRightText;

	private OnTitleBarItemClickListener mOnItemClickListener;

	public interface OnTitleBarItemClickListener {
		void onLeftItemClick();

		void onRightItemClick();
	}

	public CommonTitleBar(Context context) {
		this(context, null);
	}

	public CommonTitleBar(Context context, @Nullable AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public CommonTitleBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CommonTitleBar);
		mTitleName = typedArray.getString(R.styleable.CommonTitleBar_title_name);
		mBackgroundColor = typedArray.getColor(R.styleable.CommonTitleBar_title_background, Color.WHITE);
		mRightImageSrcRes = typedArray.getResourceId(R.styleable.CommonTitleBar_right_src, 0);
		mRightText = typedArray.getString(R.styleable.CommonTitleBar_right_text);

		typedArray.recycle();
		init(context);
	}

	private void init(Context context) {
		mContext = context;
		View view = LayoutInflater.from(context).inflate(R.layout.common_title_bar, this);

		mLlTitle = (LinearLayout) view.findViewById(R.id.rl_title);
		mIvBack = (ImageView) view.findViewById(R.id.iv_back);
		mCustomTitleContainer = (RelativeLayout) view.findViewById(R.id.custom_title_container);
		mTvTitle = (TextView) view.findViewById(R.id.tv_title);
		mRightImage = (ImageView) view.findViewById(R.id.iv_right);
		mSpace = (View) view.findViewById(R.id.space);
		mRightTextView = (TextView) view.findViewById(R.id.tv_right);

		mIvBack.setOnClickListener(this);
		mRightImage.setOnClickListener(this);
		mRightTextView.setOnClickListener(this);
		view.setOnClickListener(this);

		mLlTitle.setBackgroundColor(mBackgroundColor);
		mSpace.setBackgroundColor(mBackgroundColor);

		mRightTextView.setVisibility(View.GONE);
		mRightImage.setVisibility(View.INVISIBLE);

		if (TextUtils.isEmpty(mTitleName)) {
			mTvTitle.setVisibility(View.INVISIBLE);
		} else {
			mTvTitle.setText(mTitleName);
		}

		if (mRightImageSrcRes != 0) {
			mRightImage.setVisibility(View.VISIBLE);
			mRightTextView.setVisibility(View.GONE);
			setRightImageRes(mRightImageSrcRes);
		}

		if (!TextUtils.isEmpty(mRightText)) {
			mRightTextView.setVisibility(View.VISIBLE);
			mRightImage.setVisibility(View.GONE);
			setRightText(mRightText);
		}
	}

	public void setOnTitleBarItemClickListener(OnTitleBarItemClickListener onItemClickListener) {
		mOnItemClickListener = onItemClickListener;
	}

	public void setCustomTitleView(View view, ViewGroup.LayoutParams layoutParams) {
		mCustomTitleContainer.setVisibility(View.VISIBLE);
		mTvTitle.setVisibility(View.GONE);
		mCustomTitleContainer.addView(view, layoutParams);
	}

	public void setTitleBarBackgroundColor(int color) {
		mLlTitle.setBackgroundColor(color);
		mSpace.setBackgroundColor(color);
	}

	public void setTitleBarBackgroundResource(int resId) {
		mLlTitle.setBackgroundResource(resId);
		mSpace.setBackgroundResource(resId);
	}

	public void setRightText(String text) {
		mRightTextView.setVisibility(View.VISIBLE);
		mRightImage.setVisibility(View.GONE);
		mRightTextView.setText(text);
	}

	public String getRightText() {
		return mRightTextView.getText().toString();
	}

	public void setRightTextColor(int color) {
		mRightTextView.setTextColor(color);
	}

	public void setRightTextEnableClick(boolean enable) {
		mRightTextView.setEnabled(enable);
	}

	public void setRightImageVisibility(int viewVisibility) {
		mRightImage.setVisibility(viewVisibility);
	}

	public void setRightImageRes(int res) {
		mRightImage.setImageResource(res);
	}

	public void setRightTextVisibility(int visibility) {
		mRightImage.setVisibility(View.GONE);
		if (visibility == View.GONE) {
			visibility = View.INVISIBLE;
		}
		mRightTextView.setVisibility(visibility);
	}

	public int getRightTextVisibility() {
		return mRightTextView.getVisibility();
	}

	public void setTitleName(String titleName) {
		hideCustomTitleContainerView(true);
		mTvTitle.setText(titleName);
	}

	public void showCustomTitleContainerView() {
		mCustomTitleContainer.setVisibility(View.VISIBLE);
		mTvTitle.setVisibility(View.GONE);
	}

	public void hideCustomTitleContainerView(boolean isShowTitleName) {
		mCustomTitleContainer.setVisibility(View.GONE);
		mTvTitle.setVisibility(isShowTitleName ? View.VISIBLE : View.INVISIBLE);
	}

	public void showTitleName() {
		hideCustomTitleContainerView(true);
	}

	public void hideTitleName() {
		hideCustomTitleContainerView(false);
	}

	public void setTitleNameVisibility(int visibility) {
		mTvTitle.setVisibility(visibility);
	}

	public void setTitleTextColor(int color) {
		mTvTitle.setTextColor(color);
	}

	public void fitStatusBar(int height) {
		MarginLayoutParams marginLayoutParams = (MarginLayoutParams) mSpace.getLayoutParams();
		marginLayoutParams.height = height;
		mSpace.setLayoutParams(marginLayoutParams);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.iv_back:
				if (mOnItemClickListener != null) {
					mOnItemClickListener.onLeftItemClick();
				}
				break;
			case R.id.iv_right:
			case R.id.tv_right:
				if (mOnItemClickListener != null) {
					mOnItemClickListener.onRightItemClick();
				}
				break;
		}
	}

}
